package me.firstapp.service.weixin.system.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.StrUtils;
import me.firstapp.module.weixin.system.Broadcast;
import me.firstapp.repository.weixin.system.BroadcastRepository;
import me.firstapp.service.weixin.system.BroadcastService;

@Service
public class BroadcastServiceImpl implements BroadcastService {

	@Autowired
	private BroadcastRepository broadcastRepository;

	@Override
	public Page<Broadcast> findBroadcastPage(Integer pageNo, Integer pageSize, Map<String, Object> searchParams,
			String sortParam, String sortType) {
		return broadcastRepository.findBroadcastPage(pageNo, pageSize, searchParams, sortParam, sortType);
	}

	@Override
	public Broadcast findById(Long id) {
		return (Broadcast) broadcastRepository.getById(id);
	}

	@Override
	public Broadcast executeAdd(String type, String name, String backgroundColor, String url, Integer sort,
			String remark) {
		Broadcast broadcast = new Broadcast();
		broadcast.setType(type);
		broadcast.setName(name);
		broadcast.setBackgroundColor(backgroundColor);
		broadcast.setUrl(url);
		broadcast.setSort(sort);
		broadcast.setStatus(Broadcast.STATUS_OFF);
		broadcast.setRemark(remark);
		broadcastRepository.save(broadcast);
		return broadcast;
	}

	@Override
	public Broadcast executeUpdate(Long id, String type, String name, String backgroundColor, String url, Integer sort,
			Integer status, String remark) {
		Broadcast broadcast = (Broadcast) broadcastRepository.getById(id);
		if (null == broadcast) {
			throw new ServiceException(StatusHouse.COMMON_PARAMETER_ERROR);
		}
		if (StrUtils.isNotNULL(type)) {
			broadcast.setType(type);
		}
		if (StrUtils.isNotNULL(name)) {
			broadcast.setName(name);
		}
		if (StrUtils.isNotNULL(backgroundColor)) {
			broadcast.setBackgroundColor(backgroundColor);
		}
		if (StrUtils.isNotNULL(url)) {
			broadcast.setUrl(url);
		}
		if (null != sort) {
			broadcast.setSort(sort);
		}
		if (null != status) {
			broadcast.setStatus(status);
		}
		if (StrUtils.isNotNULL(remark)) {
			broadcast.setRemark(remark);
		}
		return broadcast;
	}

	@Override
	public List<Broadcast> findBroadcasts() {
		return broadcastRepository.findBroadcasts();
	}

}
